#include<stdio.h>  
#include<easyx.h>
#include<graphics.h>
#include<Windows.h>
#include<mmsystem.h>
#include<math.h>
#pragma comment(lib,"winmm.lib")
int p = 0;
int map[20][20] = { 0 };
void initGame()
//游戏准备工作
{
	initgraph(700, 500); 
	//绘制游戏窗口

	setbkcolor(RGB(115, 251, 253));
	cleardevice();
	//设置背景颜色
	
	IMAGE img;
	loadimage(&img, "12.jpg");
	putimage(0, 0, &img);
	//设置背景图片

	setlinecolor(BLACK);
	for (int i = 0; i < 20; i++)
	{
		line(0, i * 25, 500, i * 25);
		line(i*25, 0, i*25, 500);
		
	}
	line(500, 0, 500, 500);
	//画出棋盘格子

	setbkmode(0);
	settextcolor(BLACK);
	outtextxy(520, 20, "玩家1：白棋");
	outtextxy(520, 70, "玩家2：黑棋");
	//添加游戏备注

	mciSendString("open 55.mp3 alias bgm", NULL, 0, NULL);
	mciSendString("play bgm", NULL, 0, NULL);
	mciSendString("setaudio bgm volume to 50", NULL, 0, NULL);
	//添加游戏背景音乐
}


int judge(int MAPX, int MAPY)
{
	for (int u = MAPX - 4; u <= MAPX; u++)
	{
		if ((u>=1)&&(u<16)&&(map[u][MAPY] ==1)&& (map[u + 1][MAPY] == 1 )&&
			(map[u + 2][MAPY] == 1) && (map[u + 3][MAPY] == 1) && (map[u+ 4][MAPY] == 1))
		{
			return 1;
		}
	}
	for (int u = MAPX - 4; u <= MAPX; u++)
	{
		if ((u >= 1) && (u < 16) && (map[u][MAPY] == 2) && (map[u + 1][MAPY] == 2) && 
			(map[u + 2][MAPY] == 2) && (map[u + 3][MAPY] == 2) && (map[u + 4][MAPY] == 2))
		{
			return 2;
		}
	}
	for (int u = MAPY - 4; u <= MAPY; u++)
	{
		if ((u >= 1) && (u < 16) && (map[MAPX][u] == 1) && (map[MAPX][u+1] == 1) &&
			(map[MAPX][u+4] == 1) && (map[MAPX][u+2] == 1) && (map[MAPX][u+3] == 1))
		{
			return 1;
		}
	}
	for (int u = MAPY - 4; u <= MAPY; u++)
	{
		if ((u >= 1) && (u < 16) && (map[MAPX][u] == 2) && (map[MAPX][u + 1] == 2) 
			&& (map[MAPX][u + 4] == 2) && (map[MAPX][u + 2] == 2) && (map[MAPX][u + 3] == 2))
		{
			return 2;
		}
	}
	for (int u = MAPY - 4; u <= MAPY; u++)
	{
		if ((u >= 1) && (u < 16) && (map[MAPX+(u-MAPY)][u] == 1) && (map[MAPX + (u - MAPY)][u + 1] == 1) &&
			(map[MAPX + (u - MAPY)][u + 4] == 1) && (map[MAPX + (u - MAPY)][u + 2] == 1) && (map[MAPX + (u - MAPY)][u + 3] == 1))
		{
			return 1;
		}
	}


};


void playGame()
//开始游戏
{
	MOUSEMSG Msg;
	int L = 0;
	int M = 0;
	int MAPX ;
	int MAPY;
	while (1) {
		Msg = GetMouseMsg();//捕捉鼠标位置
		//if(Msg.uMsg==WM_LBUTTONDOWN)
		//{
		//	printf("X:%d,Y:%d\n",Msg.x,Msg.y);
		//	mciSendString("open as.mp3 alias ty", NULL, 0, NULL);
		//	mciSendString("play ty", NULL, 0, NULL);
		//	mciSendString("setaudio ty volume to 900", NULL, 0, NULL);
		//}
		for (int i = 1; i < 20; i++)
		{
			for (int j = 1; j < 20; j++)
			{
				if (abs(Msg.x - j * 25) < 12 && abs(Msg.y - i * 25) < 12)
				{
					L = j * 25;
					M = i * 25;
					MAPX = j;
					MAPY = i;

				}
			}
		}
		printf("X=%d,Y=%d\n", L, M);
		//优化鼠标位置

		if (Msg.uMsg == WM_LBUTTONDOWN)
		{
			setfillcolor(WHITE);
			solidcircle(L, M, 10);
			p = 0;
			map[MAPX][MAPY] = 1;
			if (judge(MAPX, MAPY) == 1)
			{


	
				MessageBox(NULL, "玩家1白棋胜利", "game over", MB_OK);
				return;
			}

		}
		if (Msg.uMsg == WM_RBUTTONDOWN)
		{
			setfillcolor(BLACK);
			solidcircle(L, M, 10);
			p = 1;
			map[MAPX][MAPY] = 2;
			if (judge(MAPX, MAPY) == 2)
			{


				//else if (Msg.uMsg == WM_RBUTTONDOWN)
				//{
				MessageBox(NULL, "玩家2黑棋胜利", "game over", MB_OK);
				return;
			}
		}//区分两个玩家
		

		//	mciSendString("open tss.mp3 alias py", NULL, 0, NULL);
		//	mciSendString("play py", NULL, 0, NULL);
		//	mciSendString("setaudio py volume to 900", NULL, 0, NULL);
		//}

	}
	
}




int main() {
	
	initGame();
	playGame();
	getchar();
	return 0;

}
